package com.cisystem.system.service.impl;

import java.util.List;
import java.util.regex.Pattern;

import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.cisystem.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cisystem.system.mapper.SysMailTemplateMapper;
import com.cisystem.system.domain.SysMailTemplate;
import com.cisystem.system.service.ISysMailTemplateService;

/**
 * 邮件模版Service业务层处理
 *
 * @author ruoyi
 * @date 2023-10-24
 */
@Service
public class SysMailTemplateServiceImpl implements ISysMailTemplateService
{
    /**
     * 正则表达式，匹配 {} 中的变量
     */
    private static final Pattern PATTERN_PARAMS = Pattern.compile("\\{(.*?)}");
    @Autowired
    private SysMailTemplateMapper sysMailTemplateMapper;

    /**
     * 查询邮件模版
     *
     * @param id 邮件模版主键
     * @return 邮件模版
     */
    @Override
    public SysMailTemplate selectSysMailTemplateById(Long id)
    {
        return sysMailTemplateMapper.selectSysMailTemplateById(id);
    }

    /**
     * 查询邮件模版列表
     *
     * @param sysMailTemplate 邮件模版
     * @return 邮件模版
     */
    @Override
    public List<SysMailTemplate> selectSysMailTemplateList(SysMailTemplate sysMailTemplate)
    {
        return sysMailTemplateMapper.selectSysMailTemplateList(sysMailTemplate);
    }

    /**
     * 新增邮件模版
     *
     * @param sysMailTemplate 邮件模版
     * @return 结果
     */
    @Override
    public int insertSysMailTemplate(SysMailTemplate sysMailTemplate)
    {
        sysMailTemplate.setCreateTime(DateUtils.getNowDate());
        String mailParams =JSONUtil.parseObj(parseTemplateContentParams(sysMailTemplate.getContent())).toStringPretty();
        sysMailTemplate.setMailParams(mailParams);
        return sysMailTemplateMapper.insertSysMailTemplate(sysMailTemplate);
    }

    public List<String> parseTemplateContentParams(String content) {
        return ReUtil.findAllGroup1(PATTERN_PARAMS, content);
    }

    /**
     * 修改邮件模版
     *
     * @param sysMailTemplate 邮件模版
     * @return 结果
     */
    @Override
    public int updateSysMailTemplate(SysMailTemplate sysMailTemplate)
    {
        sysMailTemplate.setUpdateTime(DateUtils.getNowDate());



        List<String> list =parseTemplateContentParams(sysMailTemplate.getContent());



        String mailParams =JSONUtil.parseArray(list).toStringPretty();


        sysMailTemplate.setMailParams(mailParams);
        return sysMailTemplateMapper.updateSysMailTemplate(sysMailTemplate);
    }

    /**
     * 批量删除邮件模版
     *
     * @param ids 需要删除的邮件模版主键
     * @return 结果
     */
    @Override
    public int deleteSysMailTemplateByIds(Long[] ids)
    {
        return sysMailTemplateMapper.deleteSysMailTemplateByIds(ids);
    }

    /**
     * 删除邮件模版信息
     *
     * @param id 邮件模版主键
     * @return 结果
     */
    @Override
    public int deleteSysMailTemplateById(Long id)
    {
        return sysMailTemplateMapper.deleteSysMailTemplateById(id);
    }
}